<?php

class PaymentAction extends PublicAction {

    protected $db,$path;

    function _initialize()
    {
        parent::_initialize();
        $this->path = './Grzx/Lib/Pay/';
        $this->db = M('Payment');
    }

    function index()
    {
        $tempfiles = dir_list($this->path,'php');

        $list = $this->db->Field('id,pay_code,status,listorder,pay_name')->select();
        foreach((array)$list as $key=>$r){
            $installed[$r['pay_code']] = $r;
        }
        foreach($tempfiles as $r){
            $filename = basename($r);
            $pay_code = str_replace('.class.php','',$filename);

            import("@.Pay.".$pay_code);
            $pay=new $pay_code();
            $paylist[$pay_code] = $pay->setup();
            if($installed[$pay_code]){
                $paylist[$pay_code]['id'] = $installed[$pay_code]['id'];
                $paylist[$pay_code]['status'] = $installed[$pay_code]['status'];
                $paylist[$pay_code]['listorder'] = $installed[$pay_code]['listorder'];
                $paylist[$pay_code]['pay_name'] = $installed[$pay_code]['pay_name'];
            }
        }

        $this->assign('list',$paylist);
        $this->display();
    }

    function add()
    {
        $code = $_REQUEST['code'];
        if(is_file($this->path.$code.'.class.php')){
          import("@.Pay.".$code);
          $pay=new $code();
          $setup = $pay->setup();
          $this->assign('vo',$setup);
        }else{
          $this->error(L('do_empty'));
        }

        $this->display ('edit');
    }

    function edit()
    {
        $id=intval($_REQUEST['id']);
        $data = $this->db->find($id);
        $data['pay_config'] = unserialize($data['pay_config']);
        $code= $data['pay_code'];
        if(is_file($this->path.$code.'.class.php')){
            import("@.Pay.".$code);
            $pay=new $code();
            $setup = $pay->setup();
        }
        foreach($setup['config'] as $key=>$r){
          $r['value'] = $data['pay_config'][$r['name']];
          $setup['config'][$key] = $r;
        }
        $data = $data+$setup;
        $this->assign('vo',$data);
        $this->display ();
    }

    function _before_insert()
    {
        $_POST['pay_config']=serialize($_POST['pay_config']);
        $_POST['pay_fee'] = $_POST['pay_fee_type'] ? $_POST['pay_fix'] : $_POST['pay_rate'] ;

    }

    function update()
    {
        $_POST['pay_config'] = serialize($_POST['pay_config']);
        $_POST['pay_fee'] = $_POST['pay_fee_type'] ? $_POST['pay_fix'] : $_POST['pay_rate'] ;


        if($_POST['setup'])
            $_POST['setup'] = array2string($_POST['setup']);

        $name = MODULE_NAME;
        $model = D( $name );

        if (false === $model->create()) {
            $this->error($model->getError ());
        }

        if (false !== $model->save()) {

            if(in_array(ucfirst($name),$this->cache_model))
                savecache($name);

            if($_POST['aid']){
                $Attachment =M('attachment');
                $aids =  implode(',',$_POST['aid']);
                $data['id'] = $_POST['id'];
                $data['catid'] = intval($_POST['catid']);
                $data['status'] = '1';
                $Attachment->where("aid in (".$aids.")")->save($data);
            }

            if($_POST['isajax'])
                $this->assign('dialog','1');

            $jumpUrl = $_POST['forward'] ? $_POST['forward'] : U(MODULE_NAME.'/index');

            $this->assign('jumpUrl', $jumpUrl);
            $this->success(L('edit_ok'));
        } else {
            $this->success (L('edit_error').': '.$model->getDbError());
        }
    }

}